﻿#pragma once
#include "HookModule.h"
#include <Windows.h>
#include <cstdint>
#include <string>
#include <atomic>
#include <mutex>
#include <vector>
#include <memory>

/**
 * Qt 函数钩子类，用于拦截并修改 IDA 中的字符串显示
 *
 * 拦截 Qt 的核心字符串处理函数，实现对界面文本的翻译和替换
 */
class IDAHook : public IHookModule {
public:
    bool Install() override;
    bool Uninstall() override;
    const char* Name() const override { return "IDAHook"; }

private:
    // Qt 函数指针类型定义
    using QStringConstructor = void* (__fastcall*)(void* thisPtr, const wchar_t* str, int length); // 构造函数
    using QStringDestructor = void(__fastcall*)(void* thisPtr); // 析构函数
    using QStringFromUtf8 = void(__fastcall*)(void* ret, const char* str, int size);
    using QStringFromAsciiHelper = __int64(__fastcall*)(__int64 a1, int a2);
    using QTranslate = void(__fastcall*)(void* ret, const char* context, const char* key, const char* disambiguation, int n);
    using QWindowSetTitle = void(__fastcall*)(void* pThis, const void* pQString);
    using QLabelSetText = void(__fastcall*)(void* pThis, const void* pQString);
    using QPainterDrawText = void(__fastcall*)(void* pThis, void* rect, int flags, void* text, void* boundingRect);

    // 原始函数指针
    static QStringConstructor s_pOriginalQStringConstructor;
    static QStringDestructor s_pOriginalQStringDestructor;
    static QStringFromUtf8 s_pOriginalQStringFromUtf8;
    static QStringFromAsciiHelper s_pOriginalQStringFromAsciiHelper;
    static QTranslate s_pOriginalQTranslate;
    static QWindowSetTitle s_pOriginalQWindowSetTitle;
    static QLabelSetText s_pOriginalQLabelSetText;
    static QPainterDrawText s_pOriginalQPainterDrawText;

    // QString 内部结构偏移常量 (64位系统)
    static constexpr uintptr_t STRING_LENGTH_OFFSET = 0x4;    // 长度字段偏移
    static constexpr uintptr_t STRING_CAPACITY_OFFSET = 0x8;  // 容量字段偏移
    static constexpr uintptr_t STRING_DATA_OFFSET = 0x18;     // 数据指针偏移
    static constexpr size_t QSTRING_OBJECT_SIZE = 24;         // QString对象大小(64位系统)

    // 钩子处理函数
    static __int64 __fastcall HookedQStringFromAsciiHelper(__int64 a1, int a2);
    static void __fastcall HookedQStringFromUtf8(void* ret, const char* str, int size);
    static void __fastcall HookedQTranslate(void* ret, const char* context, const char* key, const char* disambiguation, int n);
    static void __fastcall HookedQWindowSetTitle(void* pThis, const void* pQString);
    static void __fastcall HookedQLabelSetText(void* pThis, const void* pQString);
    static void __fastcall HookedQPainterDrawText(void* pThis, void* rect, int flags, void* text, void* boundingRect);

    // QString 工具函数
    static std::wstring ExtractStringFromQString(const void* pQString);
	static bool ModifyQString(const void* pQString, const std::wstring& newContent); //慎用, 直接修改 QString 内容 
    static void* CreateTemporaryQString(const wchar_t* content);
    static void* CreateTemporaryQString(const std::wstring& content);
    static void ReleaseTemporaryQString(void* pQString);

    // 核心功能函数
    static bool LoadIDAFunctions();
};
